const { merge } = require('webpack-merge');
const path = require('path');
const commonConfig = require('./webpack.common');

const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserWebpackPlugin = require('terser-webpack-plugin');

process.env.NODE_ENV = 'production';

/**
 * @type {import('webpack').Configuration}
 */
const prodConfig = {
    mode: 'production',
    devtool: 'none',
    output: {
        filename: 'js/[name].[contentHash:8].js',
        path: path.resolve(__dirname, 'dist'),
        chunkFilename: 'js/[name].[contentHash:8].chunk.js'
    },

    optimization: {
        usedExports: true,
        minimize: true,
        minimizer: [
            new TerserWebpackPlugin({}),
            new OptimizeCssAssetsPlugin({})
        ]
    },

    plugins: [
        new CleanWebpackPlugin(),
        new CopyWebpackPlugin([{
            from: 'public'
        }])
    ]
};

module.exports = merge(commonConfig, prodConfig);